package LeetCode1;

import java.util.*;

/**
 * 最常见的单词
 *
 * @author 是阿秋啊
 * @date 2022/03/27 09:27
 **/
public class Num819 {
    public static void main(String[] args) {
        String s = "Bob!";
        String[] str = {"hit"};
        System.out.println(mostCommonWord(s, str));
    }

    public static String mostCommonWord(String paragraph, String[] banned) {
        String s = paragraph.toLowerCase();
        Map<String, Integer> map = new HashMap<>();
        int i = 0;
        int len = paragraph.length();
        while (i < len) {
            int j = i;
            while (i < len && s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
                i++;
            }
            String temp = s.substring(j, i);
            map.put(temp, map.getOrDefault(temp, 0) + 1);
            while (i < len && (s.charAt(i) < 'a' || s.charAt(i) > 'z')) {
                i++;
            }
        }
        Queue<Frid> queue = new PriorityQueue<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            Frid frid = new Frid(entry.getKey(), entry.getValue());
            queue.offer(frid);
        }
        String ret = "";
        while (!queue.isEmpty()) {
            boolean flag = true;
            ret = queue.poll().s;
            for (String value : banned) {
                if (ret.equals(value)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                break;
            }
        }
        return ret;
    }

    static class Frid implements Comparable<Frid> {
        String s;
        int times;

        public Frid(String s, int times) {
            this.s = s;
            this.times = times;
        }

        @Override
        public int compareTo(Frid o) {
            return o.times - this.times;
        }
    }
}
